package com.lans.linear;

/**
 * @author: lans
 * @date: 2025/2/19
 * @name: 刘宇
 */
public class GLinkedList{
    //头结点
    private GoodsNode node = new GoodsNode(0,"",0.0);

    /**
     * 添加节点
     */
    public void add(GoodsNode goodsNode){
        //判断是追加节点还是添加的第一个节点
        GoodsNode temp = node;
        while(true){
            if(temp.next ==null){//最后一个或第一个
                break;
            }
            temp = temp.next;
        }
        temp.next = goodsNode;
    }

    /**
     * 添加节点根据编号进行排序
     * @param goodsNode
     */
    public void addByOrder(GoodsNode goodsNode){
        //记录头结点
        GoodsNode temp = node;
        boolean flag = false;
        while(true){
            if(temp.next ==null){
                break;
            }
            //添加新节点编号大于已经存在的编号
            if(temp.next.gId>goodsNode.gId){
                break;
            }else if(temp.next.gId == goodsNode.gId){//已经存在的编号不能重复添加
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag){
            System.out.println("该商品已存在 不能重复添加");
        }else{
            goodsNode.next=temp.next;
            temp.next = goodsNode;
        }
    }

    /**
     * 根据编号进行修改
     */
    public void updateNode(GoodsNode goodsNode){
        if(node.next==null){
            System.out.println("空链表");
            return;
        }
        GoodsNode temp = node.next;
        boolean flag = false;
        while(true){
            if(temp==null){
                break;
            }
            if(temp.gId == goodsNode.gId){
                flag =  true;
                break;
            }
            temp = temp.next;
        }
        if(flag){
            temp.gName = goodsNode.gName;
            temp.gPrice = goodsNode.gPrice;
        }else{
            System.out.println("未找到要修改的节点");
        }
    }

    public void delNode(int gId){
        GoodsNode temp = node;
        boolean flag = false;
        while(true){
            if(temp==null){
                break;
            }
            if(temp.next.gId ==gId){
                flag =  true;
                break;
            }
            temp = temp.next;
        }
        if(flag){
            temp.next = temp.next.next;
        }else{

        }
    }
}